import { getDicts } from "@/api/system/dict/data";

const dict = {
    state: {
        // 包装形式字典
        waste_package_type: [],
        // 处置方式字典
        waste_handler_method: [],
        // 危废形态字典
        waste_shape: [],
        // 危险特性字典
        waste_dangerous_characteristic: [],
        // 危废状态
        waste_flow_status: [],
    },

    mutations: {
        SET_PACKAGE_TYPE_OPTIONS: (state, packageTypeOptions) => {
            state.waste_package_type = packageTypeOptions
        },
        SET_HANDLER_METHOD_OPTIONS: (state, handlerMethodOptions) => {
            state.waste_handler_method = handlerMethodOptions
        },
        SET_WASTE_SHAPE_OPTIONS: (state, wasteShapeOptions) => {
            state.waste_shape = wasteShapeOptions
        },
        SET_HAZARDOUS_CHARACTERISTICS_OPTIONS: (state, hazardousCharacteristicsOptions) => {
            state.waste_dangerous_characteristic = hazardousCharacteristicsOptions
        },

        SET_DATA_COMMON(state, param) {
            state[param.dictKey] = param.data;
        }
    },

    actions: {
        // 获取包装形式数据
        getPackageTypeOptions({ commit }) {
            return new Promise((resolve, reject) => {
                getDicts("waste_package_type").then((response) => {
                    commit("SET_PACKAGE_TYPE_OPTIONS", response.data);
                    resolve();
                }).catch(error => {
                    reject(error)
                });
            })
        },

        // 获取处置方式
        getWasteShapeOptions({ commit }) {
            return new Promise((resolve, reject) => {
                getDicts("waste_handler_method").then((response) => {
                    commit("SET_HANDLER_METHOD_OPTIONS", response.data);
                    resolve();
                }).catch(error => {
                    reject(error)
                });
            })
        },

        // 获取危废形态
        getHandlerMethodOptions({ commit }) {
            return new Promise((resolve, reject) => {
                getDicts("waste_shape").then((response) => {
                    commit("SET_WASTE_SHAPE_OPTIONS", response.data);
                    resolve();
                }).catch(error => {
                    reject(error)
                });
            })
        },

        // 获取废物特性字段数据
        getHazardousCharacteristicsOptions({ commit }) {
            return new Promise((resolve, reject) => {
                getDicts("waste_dangerous_characteristic").then((response) => {
                    commit("SET_HAZARDOUS_CHARACTERISTICS_OPTIONS", response.data);
                    resolve();
                }).catch(error => {
                    reject(error)
                });
            })
        },

        // 通用获取数据方法
        getDictDataCommon({ commit }, dictKeyArr) {
            return new Promise((resolve, reject) => {
                for (const dictKey of dictKeyArr) {
                    getDicts(dictKey).then((response) => {
                        const data = response.data;
                        commit("SET_DATA_COMMON", {dictKey, data});
                    })
                }
            }).catch(error => {
                reject(error)
            });
        },

        // wasteInfoCard页面需要用到的字典数据初始化
        initDataForWasteInfoCard({ dispatch, commit }) {
            const dictKeyArr = ["waste_package_type", "waste_handler_method", "waste_shape", "waste_dangerous_characteristic"];
            return new Promise((resolve, reject) => {
                dispatch("getDictDataCommon", dictKeyArr).then(function (data) {
                    resolve();
                })
            }).catch(error => {
                reject(error)
            });
        },

        // wasteInfoCard页面需要用到的字典数据初始化
        initData({ dispatch }) {
            const dictKeyArr = ["waste_flow_status"];
            return new Promise((resolve, reject) => {
                dispatch("getDictDataCommon", dictKeyArr).then(function (data) {
                    resolve();
                })
            }).catch(error => {
                reject(error)
            });
        }
    }
}

export default dict
